package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.mapper.TeacherMapper;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-06-27
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Override
    public Page<Teacher> getPageByCondition(Page<Teacher> page, TeacherQuery query) {
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Teacher::getGmtCreate);

        System.out.println("query = " + query);
        //判断传入了哪些条件，如果有则使用
        String name = query.getName();
        Integer level = query.getLevel();
        String joinDateBegin = query.getJoinDateBegin();
        String joinDateEnd = query.getJoinDateEnd();
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like(Teacher::getName , name);
        }
        if(level!=null){
            queryWrapper.eq(Teacher::getLevel , level);
        }
        if(!StringUtils.isEmpty(joinDateBegin)){
            queryWrapper.ge(Teacher::getJoinDate , joinDateBegin);
        }
        if(!StringUtils.isEmpty(joinDateEnd)){
            queryWrapper.le(Teacher::getJoinDate , joinDateEnd);
        }
        return  this.page(page,queryWrapper);
    }
}
